<?php
namespace app\admin\controller;
use think\Controller;
use think\Db;
class Basic extends Controller{
    public function form_basic(){
        $list = Db::table('hotel')->where("type=0")->paginate(6);
        $page = $list->render();
        $date = Db::table('hotel')->where("type=1")->paginate(6);
        $pge = $date->render();
        return view('form_basic',array("data"=>$list,"page"=>$page,"da"=>$date,"pge"=>$pge));
//        return view("form_basic");
    }
    public function select($basic_id)
    {//模糊查询
        $get=$basic_id;
        $name = $_POST["keywords"];
        if ($name == "") {//判断name值是否为空
            $row = Db::name('special_catering')->paginate(6);
//            $this->error('查询数据不能为空','index','',1);
        } else {                                                                                           //不否为空进行模糊查询
            $row = Db::table("special_catering")->where("
                name like '%{$name}'
                or name like '%{$name}'
                or name like '%{$name}%'
                ")->paginate(6);  //查询 细分类的表 找到对应的类名
        }
        $page = $row->render();
        return view('Basic/basic_show',array('bo'=>$row,'page'=>$page,'basic_id'=>$get));
    }
    public function basic_list($basic_id){//页面显示
        $get_id =$basic_id;
        $list = Db::name('special_catering')->where("hotel_id=$get_id")->paginate($listRows =6,$simple = false,$config = ['query'=>array('basic_id'=>$get_id)]);
        $cc = array();
        for($i=0;$i<count($list);$i++){
            $cc[$i]['img'] = explode("/",$list[$i]["img"]);
        }
        $page=$list->render();
        return view('basic_list',array("cc"=>$cc,"data"=>$list,"page"=>$page,"basic_id"=>$get_id));
    }
    public function basic_add($basic_id){
        $get = $basic_id;
        $date=Db::table("hotel")->where("id=".$get)->select();
        $file = request()->file("file");
        if (request()->isPost()) {
            // 获取表单上传文件 例如上传了001.jpg
            $cc="";
            foreach($file as $files) {
                $info = $files->move(ROOT_PATH . 'public' . DS . 'uploads');
                if ($info) {
                    $cc .= $info->getSaveName()."/" ;
                    $img = substr($cc,0,strlen($cc)-1);
                } else {
                    $this->success('添加失败');
                }
            }
            $date = [
                "hotel_id"=>$get,
                "name"=>$_POST["name"],
                "img"=>$img,
                "introduce"=>$_POST["introduce"],
                "amounts"=>$_POST["amounts"],
                "start"=>$_POST["start"],
                "end"=>$_POST["end"]
            ];
            $aa = Db::table("special_catering")->insert($date);
            if($aa){
//                $this->success("添加成功","basic_list",'',1);
                $list=Db::table("special_catering")->where("hotel_id=".$get)->order("id desc")->paginate($listRows=6,$simple=false,$config=['query'=>array("basic_id"=>$get)]);
                  $cc=array();
                for($i=0;$i<count($list);$i++){
                    $cc[$i]['img'] = explode("/",$list[$i]["img"]);
                }
                $page = $list->render();
                return view("Basic/basic_list",["cc"=>$cc,"data"=>$list,'basic_id'=>$get,"page"=>$page]);
//                $this->redirect('basic_list', ['basic_id'=>$get]);
            }else{
                $this->success("添加失败","basic_add",'',1);
            }
        }
        return view('basic_add',array("bo"=>$date,"basic_id"=>$get));
    }
    public function basic_show($basic_id){
        $get=$basic_id;
        $date=Db::table("special_catering")->where("id=".$get)->paginate(5);
        $cc = array();
        for($i=0;$i<count($date);$i++){
            $cc[$i]['img'] = explode("/",$date[$i]["img"]);
        }
        $page=$date->render();
        return view("Basic/basic_show",array("cc"=>$cc,"date"=>$date,"page"=>$page,"basic_id"=>$get));
//        return view('basic_show');
    }
    public function basic_update($basic_id){//修改
        $get_id=$basic_id;
        $list = Db::table('special_catering')->where("id=$get_id")->limit(0,6)->paginate(6);
        $cc=array();
        for($i=0;$i<count($list);$i++){
            $cc[$i]['img'] = explode("/",$list[$i]["img"]);
        }
        $page = $list->render();
//        var_dump($list);
        return view('basic_update',["cc"=>$cc,"data"=>$list,"page"=>$page,"basic_id"=>$get_id]);
//        return view('basic_update');

    }
    public function basic_update_show($basic_id){
        $get_id=$basic_id;
        $file = request()->file("img");
        $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
        $img  = $info->getSaveName();
        $data =array(
            'name'=> $_POST['name'],
            'img'=>$img,
            'introduce'=>$_POST['introduce'],
            'amounts'=>$_POST['amounts'],
            'start'=>$_POST['start'],
            'end'=>$_POST['end']
        );
        $text = Db::table("special_catering")->where("id=$get_id")->order('id desc')->update($data);
        if(!$text){
            $this->error('修改失败','basic_update');
        }else{
            $this->redirect('Basic/basic_show', ['basic_id'=>$get_id]);
        }
//        return view('basic_update');
    }
    public function basic_delete($basic_id)
    {//餐饮删除页面
        $get = $basic_id;
        $aa = Db::table("special_catering")->where("id=".$get)->delete();
        if ($aa) {
            $list = Db::table('special_catering')->where("id=".$get)->paginate(6);
            $cc=array();
            for($i=0;$i<count($list);$i++){
                $cc[$i]['img'] = explode("/",$list[$i]["img"]);
            }
            $page = $list->render();
            return view('Basic/basic_show',["cc"=>$cc,"date"=>$list,"page"=>$page,"basic_id"=>$get]);
        } else {
            $this->error("删除失败");
        }
    }
    public  function basic_order(){
        $list = Db::name('restaurant')->paginate(5);
        $page = $list->render();
        return view("order",array("data"=>$list,"page"=>$page));

    }

}
